package me.wangxuan.pat.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;

import lombok.Data;
import lombok.EqualsAndHashCode;
import me.wangxuan.pat.entity.Project;

@Data
@EqualsAndHashCode(callSuper = false)
public class ProjectManageDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private DefaultTableModel tableModel;
	private JTable recordTable;
	private List<Project> records;
	private JButton addButton;
	private JButton deleteButton;
	private JButton updateButton;

	public ProjectManageDialog(JFrame owner, List<Project> records) {
		super(owner, "项目管理", true); // 模态对话框
		// 初始化记录列表
		this.records = records;
		// 初始化表格模型
		tableModel = new DefaultTableModel(new Object[] { "项目ID", "项目名称", "账号参数", "接口参数","请求处理器" }, 0) {
			private static final long serialVersionUID = 1L;

			// 覆盖 isCellEditable 方法
			@Override
			public boolean isCellEditable(int row, int column) {
				// 返回 false 表示该单元格不可编辑
				// 例如，这里我们将所有单元格设为不可编辑
				return false; // 或者根据需要返回 true/false
			}
		};
		for (Project p : records) {
			tableModel.addRow(new Object[] { p.getProjectId(), p.getProjectName(), p.getAccountConfigs(),
					p.getApiCommonParams(),p.getProcessor() });
		}
		// 初始化表格
		recordTable = new JTable(tableModel);

		// 设置窗口大小
		setSize(800, 600);
		// 设置窗口布局
		setLayout(new BorderLayout());

		// 创建表格面板
		JScrollPane scrollPane = new JScrollPane(recordTable);
		add(scrollPane, BorderLayout.CENTER);

		// 创建按钮面板
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());

		// 添加按钮：增加、删除、修改、查询
		addButton = new JButton("增加");
		deleteButton = new JButton("删除");
		updateButton = new JButton("修改");

		// 添加按钮点击事件监听器 需要放到controller中去处理
//		addButton.addActionListener(new AddRecordActionListener());
//		deleteButton.addActionListener(new DeleteRecordActionListener());
//		updateButton.addActionListener(new UpdateRecordActionListener());
//		searchButton.addActionListener(new SearchRecordActionListener());

		// 将按钮添加到按钮面板
		buttonPanel.add(addButton);
		buttonPanel.add(deleteButton);
		buttonPanel.add(updateButton);

		// 将按钮面板添加到窗口底部
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public class AddRecordForm extends JDialog {
		private static final long serialVersionUID = 1L;
		private JTextField projectName;
		private RSyntaxTextArea accountConfigs;
		private RSyntaxTextArea apiCommonParams;
		private JTextField processor;
		private Project result; // 用于存储用户输入的记录

		public AddRecordForm(JFrame owner, Project p) {
			super(owner, "增加记录", true); // 模态对话框
			this.result = p;
			// 设置窗口大小
			setSize(850, 600);
			// 设置窗口布局
			setLayout(new BorderLayout());

			// 创建表单面板
			JPanel formPanel = new JPanel();
			formPanel.setLayout(new GridBagLayout());

			// 添加标签和输入框
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.fill = GridBagConstraints.HORIZONTAL; // 组件水平填充
			gbc.gridx = 0; // 列索引
			gbc.gridy = 0; // 行索引
			gbc.gridwidth = 1; // 占用 1 列
			gbc.gridheight = 1; // 占用 1 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.1; // 垂直权重
			JLabel label1 = new JLabel("项目名称:");
			label1.setHorizontalAlignment(SwingConstants.CENTER);
			formPanel.add(label1, gbc);

			gbc.gridx = 1; // 列索引
			gbc.gridy = 0; // 行索引
			gbc.gridwidth = 1; // 占用 1 列
			gbc.gridheight = 1; // 占用 1 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.1; // 垂直权重

			projectName = new JTextField();

			formPanel.add(projectName, gbc);

			gbc.gridx = 0; // 列索引
			gbc.gridy = 1; // 行索引
			gbc.gridwidth = 1; // 占用 1 列
			gbc.gridheight = 2; // 占用 1 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.8; // 垂直权重

			JLabel label2 = new JLabel("账号参数:");
			label2.setHorizontalAlignment(SwingConstants.CENTER);
			formPanel.add(label2, gbc);

			gbc.gridx = 1; // 列索引
			gbc.gridy = 1; // 行索引
			gbc.gridwidth = 1; // 占用 1 列
			gbc.gridheight = 2; // 占用 2 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.8; // 垂直权重
			accountConfigs = new RSyntaxTextArea(15, 45);
			accountConfigs.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
			accountConfigs.setCodeFoldingEnabled(false);// 关闭折叠
			// 创建一个滚动面板
			RTextScrollPane accountScrollPane = new RTextScrollPane(accountConfigs);
			accountScrollPane.setLineNumbersEnabled(false);// 关闭行号显示
			formPanel.add(accountScrollPane, gbc);

			gbc.gridx = 0; // 列索引
			gbc.gridy = 3; // 行索引
			gbc.gridwidth = 1; // 占用 1 列
			gbc.gridheight = 2; // 占用 2 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.8; // 垂直权重
			JLabel label3 = new JLabel("接口参数:");
			label3.setHorizontalAlignment(SwingConstants.CENTER);
			formPanel.add(label3, gbc);

			gbc.gridx = 1; // 列索引
			gbc.gridy = 3; // 行索引
			gbc.gridwidth = 1; // 占用 2 列
			gbc.gridheight = 2; // 占用 2 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.8; // 垂直权重
			apiCommonParams = new RSyntaxTextArea(15, 45);
			apiCommonParams.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
			apiCommonParams.setCodeFoldingEnabled(false);// 关闭折叠
			// 创建一个滚动面板
			RTextScrollPane paramsScrollPane = new RTextScrollPane(apiCommonParams);
			paramsScrollPane.setLineNumbersEnabled(false);// 关闭行号显示
			formPanel.add(paramsScrollPane, gbc);
			
			gbc.gridx = 0; // 列索引
			gbc.gridy = 5; // 行索引
			gbc.gridwidth = 1; // 占用 1 列
			gbc.gridheight = 1; // 占用 2 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.1; // 垂直权重
			JLabel label4 = new JLabel("请求处理器实现类:");
			label4.setHorizontalAlignment(SwingConstants.CENTER);
			formPanel.add(label4, gbc);
			
			gbc.gridx = 1; // 列索引
			gbc.gridy = 5; // 行索引
			gbc.gridwidth = 1; // 占用 1 列
			gbc.gridheight = 1; // 占用 1 行
			gbc.weightx = 1.0; // 水平权重
			gbc.weighty = 0.1; // 垂直权重

			processor = new JTextField();

			formPanel.add(processor, gbc);

			if (result != null) {
				projectName.setText(result.getProjectName());
				accountConfigs.setText(p.getAccountConfigs());
				apiCommonParams.setText(result.getApiCommonParams());
				processor.setText(result.getProcessor());
			}
			// 创建按钮面板
			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new FlowLayout());
			// 创建“确定”按钮
			JButton okButton = new JButton("确定");
			okButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// 获取用户输入的数据
					String pName = projectName.getText().trim();
					String accountCfg = accountConfigs.getText().trim();
					String paramsCfg = apiCommonParams.getText().trim();
					String processorName = processor.getText().trim();
					// 验证输入是否为空
					if (pName.isEmpty() || accountCfg.isEmpty() || paramsCfg.isEmpty()) {
						JOptionPane.showMessageDialog(AddRecordForm.this, "请填写所有字段！");
						return;
					}

					// 创建新记录
					if (result == null) {
						result = new Project();
					}
					result.setProjectName(pName);
					result.setAccountConfigs(accountCfg);
					result.setApiCommonParams(paramsCfg);
					result.setProcessor(processorName);
					// 关闭对话框
					dispose();

				}
			});

			// 创建“取消”按钮
			JButton cancelButton = new JButton("取消");
			cancelButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// 清空输入框
					projectName.setText("");
					accountConfigs.setText("");
					apiCommonParams.setText("");
					// 关闭对话框
					dispose();
				}
			});

			// 将按钮添加到按钮面板
			buttonPanel.add(okButton);
			buttonPanel.add(cancelButton);

			// 将表单面板和按钮面板添加到对话框
			add(formPanel, BorderLayout.CENTER);
			add(buttonPanel, BorderLayout.SOUTH);

	        pack();
		}

		// 获取用户输入的记录
		public Project getRecord() {
			return result;
		}
	}
}
